﻿#include "tcpserver.h"
#include "ui_tcpserver.h"

bool tcpserver::status=false;  //状态初始化


tcpserver::tcpserver(login *l,QWidget *parent) :
    QWidget(parent),
    l(l),
    ui(new Ui::tcpserver)
{
    ui->setupUi(this);

    //换肤
    setfacer(4);
    setWindowTitle(QString::fromLocal8Bit("后台管理系统"));
    this->setWindowIcon(QIcon(":/photo/11.jfif"));
    port=8888;
}

void tcpserver::connect_server()
{
    server =new Server(this,port);
    connect(server, &Server::updateserver, this, &tcpserver::slotupdateserver);
    ui->readtext->append(QString::fromLocal8Bit("服务器打开，后台登录成功!"));
    status=true;
}


//判断是否在线
bool tcpserver::isonline()
{
    return status;
}

void tcpserver::setfacer(int index)
{
    //换背景
    QImage _image;   //创建image
   //利用字符串拼接 完成photo
    QString str=QString(":/photo/back%4.jpg").arg(index);
   _image.load(str);
   setAutoFillBackground(true);   // 这个属性一定要设置
    QPalette pal(palette());
     pal.setBrush(QPalette::Window, QBrush(_image.scaled(size(), Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
    setPalette(pal);
}


//设置主界面login中的状态
void tcpserver::isopenserver(login *l,bool isopen)
{
    l->set_pushbutton3(isopen);
}

tcpserver::~tcpserver()
{
    close();
    delete ui;
}

//结束server发来的消息然后更新界面
void tcpserver::slotupdateserver(QString msg, int length)
{
     ui->readtext->append(msg);
}


void tcpserver::on_leave_clicked()
{
    isopenserver(l,false);
    close();
}

